package com.ruoyi.crm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.crm.domain.ContactInfo;
import com.ruoyi.crm.domain.CustomerInfo;
import com.ruoyi.crm.domain.PoolCustomer;
import com.ruoyi.crm.mapper.CustomerMapper;
import com.ruoyi.crm.service.ICustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils.like;

@RequiredArgsConstructor
@Service
public class CustomerServiceImpl implements ICustomerService {
    private final CustomerMapper customerMapper;
    /**
     * 查询客户列表
     */

    @Override
    public TableDataInfo<CustomerInfo> selectCustomerPageList(CustomerInfo customerInfo, PageQuery pageQuery) {
        Map<String, Object> params = customerInfo.getParams();
        LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();

        // 客户名
        if (StringUtils.isNotBlank(customerInfo.getCustomerName())) {
            queryWrapper.like(CustomerInfo::getCustomerName, customerInfo.getCustomerName());
        }
        // 公司规模模糊匹配（注意不要拼错类型）
        if (customerInfo.getCompanySizeId() != null && customerInfo.getCompanySizeId() > 0) {
            queryWrapper.inSql(CustomerInfo::getCompanySizeId,
                "SELECT company_size_id FROM crm_company_size WHERE company_size_name LIKE '%" + customerInfo.getCompanySizeId() + "%'");
        }
        // 负责人
        if (customerInfo.getResponsiblePersonId() != null) {
            queryWrapper.eq(CustomerInfo::getResponsiblePersonId, customerInfo.getResponsiblePersonId());
        }
        // 客户类型
        if (customerInfo.getCustomerTypeId() != null) {
            queryWrapper.eq(CustomerInfo::getCustomerTypeId, customerInfo.getCustomerTypeId());
        }
        // 时间范围
        if (params != null) {
            Object begin = params.get("beginTime");
            Object end = params.get("endTime");
            if (begin != null && end != null) {
                queryWrapper.between(CustomerInfo::getNextTime, begin, end);
            }
        }

        queryWrapper.orderByAsc(CustomerInfo::getNextTime);
            Page<CustomerInfo> page = customerMapper.selectPage(pageQuery.build(), queryWrapper);
            return TableDataInfo.build(page);

    }
    /**
     * 根据客户ID获取详细信息
     *
     * @param customerId 客户ID
     */
    @Override
    public CustomerInfo selectCustomerById(Long customerId) {
        return customerMapper.selectById(customerId);
    }


    @Override
    public List<CustomerInfo> selectCustomerList(CustomerInfo customerInfo) {
        Map<String, Object> params = customerInfo.getParams();
        LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();

        // 客户名
        if (StringUtils.isNotBlank(customerInfo.getCustomerName())) {
            queryWrapper.like(CustomerInfo::getCustomerName, customerInfo.getCustomerName());
        }
        // 公司规模模糊匹配（注意不要拼错类型）
        if (customerInfo.getCompanySizeId() != null && customerInfo.getCompanySizeId() > 0) {
            queryWrapper.inSql(CustomerInfo::getCompanySizeId,
                "SELECT company_size_id FROM company_size WHERE company_size_name LIKE '%" + customerInfo.getCompanySizeId() + "%'");
        }
        // 负责人
        if (customerInfo.getResponsiblePersonId() != null) {
            queryWrapper.eq(CustomerInfo::getResponsiblePersonId, customerInfo.getResponsiblePersonId());
        }
        // 客户类型
        if (customerInfo.getCustomerTypeId() != null) {
            queryWrapper.eq(CustomerInfo::getCustomerTypeId, customerInfo.getCustomerTypeId());
        }
        // 时间范围
        if (params != null) {
            Object begin = params.get("beginTime");
            Object end = params.get("endTime");
            if (begin != null && end != null) {
                queryWrapper.between(CustomerInfo::getNextTime, begin, end);
            }
        }
        queryWrapper.orderByAsc(CustomerInfo::getNextTime);

        return customerMapper.selectList(queryWrapper);


//        Map<String, Object> params = customerInfo.getParams();
//        return customerMapper.selectList(new LambdaQueryWrapper<CustomerInfo>()
//            .like(StringUtils.isNotBlank(customerInfo.getCustomerName()),CustomerInfo::getCustomerName,customerInfo.getCustomerName())
//            .inSql(CustomerInfo::getCompanySizeId,"select company_size_id from company_size where company_size_name like '%" + customerInfo.getCompanySizeId() + "%'")
//            .eq(customerInfo.getResponsiblePersonId() != null,CustomerInfo::getResponsiblePersonId,customerInfo.getResponsiblePersonId())
//            .eq(customerInfo.getCustomerTypeId() != null,CustomerInfo::getCustomerTypeId,customerInfo.getCustomerTypeId())
//            .between(params.get("beginTime") != null && params.get("endTime") != null,
//                CustomerInfo::getNextTime,  // 使用Lambda方法引用
//                params.get("beginTime"),
//                params.get("endTime"))
//            .orderByAsc(CustomerInfo::getNextTime));

    }
    /**
     * 新增客户列表
     */
    @Override
    public int insertCustomer(CustomerInfo customerInfo) {
        return customerMapper.insert(customerInfo);
    }
    /**
     * 修改客户列表
     */
    @Override
    public int updateCustomer(CustomerInfo customerInfo) {
        return customerMapper.updateById(customerInfo);
    }
    /**
     * 删除客户列表
     */
    @Override
    public int delectCustomer(Long customerId) {
        return customerMapper.deleteById(customerId);
    }

    public boolean insertBatch(List<CustomerInfo> list) {
        return customerMapper.insertBatch(list);
    }



}
